import pandas as pd
import plotly.graph_objs as go
from plotly.subplots import make_subplots


def visualize_metal_factors(data: pd.DataFrame, factors: dict):
    fig = make_subplots(rows=3, cols=1, shared_x=True,
                        subplot_titles=["价格走势", "因子信号", "风险指标"])

    # 价格面板
    fig.add_trace(go.Candlestick(x=data.index,
                                 open=data['open'],
                                 high=data['high'],
                                 low=data['low'],
                                 close=data['close'],
                                 name="价格"),
                  row=1, col=1)

    # 信号面板
    for name, series in factors.items():
        fig.add_trace(go.Scatter(x=series.index, y=series,
                                 mode='lines', name=name),
                      row=2, col=1)

    # 风险面板
    fig.add_trace(go.Histogram(x=data['HV20'],
                               name='波动率分布'),
                  row=3, col=1)

    fig.update_layout(height=800,
                      title="贵金属因子分析仪表盘",
                      xaxis_rangeslider_visible=False)
    return fig